﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SchoolCardSystem
{
    public partial class SelectInf : System.Web.UI.Page
    {
        //选择器
        public int select = 0;
        String userId;
        //定义数据库信息
        MySqlConnection connection = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            userId = Session["name"].ToString();
        }
        protected void selectBtn_Click(object sender, EventArgs e)
        {
            select = 1;
            string sqlstr = @"select * from cardrecord where cardId=@userId";
            //Command 查询语句
            MySqlCommand cmd = new MySqlCommand(sqlstr, connection);
            //设置@userId的类型 
            cmd.Parameters.Add(new MySqlParameter("@userId", MySqlDbType.VarChar, 11));
            cmd.Parameters["@userId"].Value = userId;
            //离线模式
            DataSet ds = new DataSet();
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            //打开数据库
            connection.Open();
            adapter.SelectCommand = cmd;
            //执行语句
            adapter.SelectCommand.ExecuteNonQuery();
            //取得数据填充到data中
            adapter.Fill(ds);
            //找到空的行 删除
            DataRow[] findnull;
            findnull = ds.Tables[0].Select("cardInTime is null", "");
            foreach (DataRow row in findnull)
            {
                ds.Tables[0].Rows.Remove(row);
            }
            repeat.DataSource = ds;
            repeat.DataBind();
            //selectView.DataSource = tb;
            //selectView.DataBind();
            connection.Close();
        }

        protected void removeBtn_Click(object sender, EventArgs e)
        {
            select = 2;
            string sqlstr = @"select * from cardrecord where cardId=@userId";
            //Command 查询语句
            MySqlCommand cmd = new MySqlCommand(sqlstr, connection);
            //设置@userId的类型 
            cmd.Parameters.Add(new MySqlParameter("@userId", MySqlDbType.VarChar, 11));
            cmd.Parameters["@userId"].Value = userId;
            //离线模式
            DataSet ds = new DataSet();
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            //打开数据库
            connection.Open();
            adapter.SelectCommand = cmd;
            //执行语句
            adapter.SelectCommand.ExecuteNonQuery();
            //取得数据填充到data中
            adapter.Fill(ds);
            //找到空的行 删除
            DataRow[] findnull;
            findnull = ds.Tables[0].Select("cardOutTime is null", "");
            foreach (DataRow row in findnull) {
                ds.Tables[0].Rows.Remove(row);
            }
            repeat.DataSource = ds;
            repeat.DataBind();
            //DataTable tb = new DataTable();
            //tb = ds.Tables[0];
            //tb.Columns["cardOutPrice"].ColumnName = "消费";
            //tb.Columns["cardOutTime"].ColumnName = "消费时间";


            //tb.Columns.Remove("cardId");
            //tb.Columns.Remove("cardInTime");
            //tb.Columns.Remove("cardInPrice");
            //LabelCardOutPrice.Text = tb.Rows[0].Field<Decimal>("消费").ToString();    
            connection.Close();
        }

    }
}